/**
 * Author: Balaram Putta
 * Date: Oct 22, 2003
 * Time: 12:06:43 PM
 */
package com.scalar.chiptrack.tags;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;

import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.utils.ConfigManager;

/**
 * Custom tag that retrieves and renders more technical information
 * about the error occured
 *
 * @author Balaram Putta
 * @version
 */
public class MoreErrorInfoTag extends org.apache.struts.taglib.html.ErrorsTag
                              implements SessionKeys
{

    private final static String FONT_COLOR = "red";
    private static String ERROR_MESSAGE_CONTACT_SYS_ADMN = "Click <a href='javascript:showMoreErrorInfoPage()'>here</a> for more info";

    /**
     * class property
     */
    private String mStyleClass = "";

    public String getStyleClass()
    {
        return mStyleClass;
    }

    public void setStyleClass( String styleClass )
    {
        if( styleClass != null )
        {
            mStyleClass = styleClass;
        }
    }


    /**
     * This method writes a "Click here to view more info" link on to the JSP Page,
     * if it finds <strong>ERROR_REPORT</strong> attribute key in the user session.
     *
     * @return EVAL_BODY_INCLUDE after successful execution
     * @throws JspException If there is any problem rendering page
     */
    public int doStartTag() throws JspException
    {
        HashMap errorMessages = ( HashMap ) pageContext.getRequest().getAttribute ( ERROR_MESSAGES );
        Object errorReport = pageContext.getSession().getAttribute ( ERROR_REPORT_OBJECT );

        if( ( errorMessages != null ) && ( errorMessages.isEmpty() == false ) && ( errorReport != null ) )
        {
            // Render the message
            StringBuffer report = new StringBuffer();

            // Write the required javascrip functoin into the JSP page
            report.append( "<SCRIPT language='JavaScript'>" );
            report.append( "function showMoreErrorInfoPage()" );
            report.append( "{" );
            report.append( "newWindow = window.open ('/" + ConfigManager.getChipTracContext() + "/reportProblem.do?action=shwoErrorInfoPage','moreErrorInfoWindow','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=600,height=380,resizable=no');" );
            report.append( "newWindow.focus();" );
            report.append( "}" );
            report.append( "</SCRIPT>" );

            report.append( "<table border='0' bordercolor='#FFFFFF' class='" + mStyleClass + "'>" );
            report.append( "<tr><td align='left'><font color='" + FONT_COLOR + "'><bold><li>" + ERROR_MESSAGE_CONTACT_SYS_ADMN );
            report.append( "</table>" );

            try
            {
                pageContext.getOut().print( report.toString() );
            }
            catch( IOException ioe )
            {
                System.out.println( "Exception while rendering error messages" );
                System.out.println( "Exception Message: " + ioe.getMessage() );
            }
        }

        return EVAL_BODY_INCLUDE;
    }

}